<template>
  <d2-container>
    <el-form :inline="true" :model="schoolListSearch" ref="schoolListSearch">
      <el-form-item label="校区名字" prop="name">
        <el-input v-model="schoolListSearch.name" placeholder="请输入校区名字"></el-input>
      </el-form-item>
      <el-form-item label="缴费状态：" prop="type">
        <el-radio-group v-model="schoolListSearch.type">
          <el-radio :label="2">全部</el-radio>
          <el-radio :label="1">已缴费</el-radio>
          <el-radio :label="0">未缴费</el-radio>
        </el-radio-group>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="formSubmit">
          <d2-icon name="search" />查询
        </el-button>
      </el-form-item>
      <el-form-item>
        <el-button @click="formReset">
          <d2-icon name="refresh" />重置
        </el-button>
      </el-form-item>
      <a href="#/school-new">
        <el-button type="primary" style="float:right" icon="el-icon-plus">新建校区</el-button>
      </a>
    </el-form>
    <el-table :data="schoolList" border style="width: 100%">
      <el-table-column prop="name" label="校区名称"></el-table-column>
      <el-table-column prop="studentLimit" label="学员名额"></el-table-column>
      <el-table-column prop="startText" label="开通日期"></el-table-column>
      <el-table-column prop="deadlineText" label="到期日期"></el-table-column>
      <el-table-column prop="payAmount" label="缴费金额"></el-table-column>
      <el-table-column prop="payStatusText" label="缴费状态"></el-table-column>
      <el-table-column fixed="right" label="操作">
        <template slot-scope="scope">
          <el-button @click="handleClick(scope.row)" type="text" size="small">查看</el-button>
          <el-button @click="handleClick2(scope.row)" type="text" size="small">详情</el-button>
          <el-button @click="deleteClick(scope.row)" type="text" size="small" v-if="scope.row._id != '5d7515647ada0f122e442b6a'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <el-pagination
      background
      layout="total,sizes,prev, pager, next,jumper"
      :page-size="pageSize"
      :current-page="currentPage"
      :total="total"
      @current-change="handleCurrentChange"
      @size-change="handlerSizeChange"
      @next-click="handleNextPage"
      @prev-click="handlePrevClick"
    ></el-pagination>
  </d2-container>
</template>

<script>
import request from "@/plugin/axios";
import { constants } from 'crypto';
export default {
  data() {
    return {
      schoolList: [],
      total: 0,
      pageSize: 20,
      currentPage: 1,
      schoolListSearch: {
        name: "",
        type: 2
      }
    };
  },
  methods: {
    formSubmit() {
      this.getSchoolList(1, 10);
    },
    formReset() {
      this.schoolListSearch = {}
      this.getSchoolList(1, 10);
    },
    handleClick(e) {
      this.$router.push({ path: "/school-detail?id=" + e._id });
    },
    handleClick2(e) {
      this.$router.push({ path: "/school-info?id=" + e._id });
    },
    deleteClick(e) {
      this.$confirm("此操作将删除该校区, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      }).then(() => {
        request({
          url: "/school/deleteSchool",
          method: "post",
          data: {id: e._id}
        }).then(res => {
          if (res.code == 200) {
            this.getSchoolList(1, this.pageSize);
            this.$message({
              message: "删除成功"
            });
          }
        });
      }).catch(() => {
                this.$message({
                  type: "info",
                  message: "已取消删除"
                });
              });
    },
    handleCurrentChange(e) {
      this.getSchoolList(e, this.pageSize);
    },
    handlerSizeChange(e) {
      this.pageSize = e;
      this.getSchoolList(1, this.pageSize);
    },
    handleNextPage(e) {
      this.getSchoolList(e, this.pageSize);
    },
    handlePrevClick(e) {
      this.getSchoolList(e, this.pageSize);
    },
    getSchoolList(page, pageSize) {
      var data = {};
      if (this.schoolListSearch.name != "") {
        data.name = this.schoolListSearch.name;
      }
      if (this.schoolListSearch.type != 2) {
        data.type = this.schoolListSearch.type;
      }
      (data.page = page), (data.pageSize = pageSize);
      request({
        url: "/school/getSchoolList",
        method: "post",
        data
      }).then(res => {
        for (var i = 0; i < res.data.school.length; i++) {
          res.data.school[i].deadlineText = this.formatDate(res.data.school[i].deadline);
                    res.data.school[i].startText = this.formatDate(res.data.school[i].startDate);

          if (res.data.school[i].payStatus == 1) {
            res.data.school[i].payStatusText = "已缴费";
          } else if (res.data.school[i].payStatus == 0) {
            res.data.school[i].payStatusText = "未缴费";
          }
        }
        this.schoolList = res.data.school;
        this.total = res.data.total
        this.currentPage = res.data.page

        // 返回数据
      });
    },
    formatDate(date) {
      var date = new Date(date);
      var year = date.getFullYear();
      var month = date.getMonth() + 1;
      var day = date.getDate();
      return [year, month, day].map(this.formatNumber).join("-");
    },
    formatNumber(n) {
      n = n.toString();
      return n[1] ? n : "0" + n;
    },
    load(){
      this.getSchoolList(1, this.pageSize);
    }
  },
   beforeRouteEnter(to,from,next){
            if (to.meta.refresh == 'a'){
                to.meta.refresh = undefined
                next(v=>{
                    v.load()
                })
            }
            else{
                next()
            }

        },
  created() {
    this.load()
  }
};
</script>
